package com.example.mall.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.example.mall.R
import com.example.mall.entity.CartInfo


class CartAdapter(var mContext: Context, var mCartList: List<CartInfo>) : BaseAdapter() {
    override fun getCount(): Int {
        return  mCartList.size
    }

    override fun getItem(position: Int): Any {
        return mCartList.get(position)
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var holder : ViewHolder? = null
        var view = convertView
        if(view == null){
            holder = ViewHolder()
            view = LayoutInflater.from(mContext).inflate(R.layout.item_cart,null)
            holder.iv_thumb = view.findViewById<ImageView>(R.id.iv_thumb)
            holder.tv_name = view.findViewById<TextView>(R.id.tv_name)
            holder.tv_desc = view.findViewById<TextView>(R.id.tv_desc)
            holder.tv_count = view.findViewById<TextView>(R.id.tv_count)
            holder.tv_price = view.findViewById<TextView>(R.id.tv_price)
            holder.tv_sum = view.findViewById<TextView>(R.id.tv_sum)

            view?.tag = holder
        }else{
            holder = convertView?.getTag() as ViewHolder
        }
        val info = mCartList.get(position)
        //holder.iv_thumb?.setImageURI(Uri.parse(info.goods?.picPath))
        Glide.with(view!!.context).load(info.goods?.picPath).into(holder.iv_thumb!!)
        holder.tv_name?.setText(info.goods?.name)
        holder.tv_desc?.setText(info.goods?.description)
        holder.tv_count?.setText(info.count.toString())
        holder.tv_price?.setText((info.goods?.price)!!.toInt().toString())
        holder.tv_sum?.setText((info.count * info.goods!!.price).toInt().toString())

        return view
    }

    class ViewHolder(
        var iv_thumb : ImageView? = null,
        var tv_name : TextView? = null,
        var tv_desc : TextView? = null,
        var tv_count : TextView? = null,
        var tv_price : TextView? = null,
        var tv_sum : TextView? = null,
    )
}